var watchPayStatus = null;
var getInput = function(name){
  return $('[name='+name+']').val()
}
// 报事收款
$(function(){
  $('.cashier .get-pay-btn').on('click', function(e){
    switch($(':checked[name=payType]').val()){
      case 'qrcode':
        location.href = '/room/cashier/paycode/' + ($(this).data('id'))
        break;
      case 'cash':
        var _totalFee = $(this).data('totalfee'),
            _id = $(this).data('id');
        Modal.confirm({
          message: Template.compile('#payTempl', {totalFee: _totalFee}),
          callback: function () {
            $.get('/room/cashPay', {
              orderCode: getInput('cashPayCode')
            }).then(function(res){
              if(res.code === 200){
                location.href = '/room/paysuccess?totalFee='+_totalFee+'&orderCode='+getInput('cashPayCode')+'&payType=现金&id='+_id
              }else{
                notice(res.data.message)
              }
            })
          }
        });
        break;
      default:
        break;

    }
  })  
})


// 监听收款二维码支付状态
$(function(){
  if($('.pay-code').length){
    var getPayStatus = function(){
      $.get('/room/ajax/bepaid/'+getInput('qrCodeId')).then(function(res){
        // 支付成功了
        var data = res.data.matterPaidOrderInfo
        if(data.paid){
          clearInterval(watchPayStatus)
          data.price = (parseFloat(data.price)/100).toFixed(2)
          location.href = '/room/paysuccess?totalFee='+data.price+'&orderCode='+data.orderCode+'&payType=扫码支付&id='+getInput('matterId')
        }
      }, function(rej){

      })
    }
    watchPayStatus = setInterval(getPayStatus, 3000)
  }
})

// 收款成功返回
$(function(){
  $('#goHome').on('click', function(e){
    var id = $(this).data('id');
    if(window.WebViewJavascriptBridge){
      window.WebViewJavascriptBridge.callHandler('jsAction',{
        'action': 'matterPaySuccess',
        'entity': {
          'id': id
        }
      });
      window.WebViewJavascriptBridge.callHandler('jsToolCloseWebPage');
    }
  })
})